import request from '@/utils/request'

// ==================== 考试管理 ====================

// 获取考试列表
export function getExamList(params) {
  return request({
    url: '/v1/exams',
    method: 'get',
    params
  })
}

// 获取考试详情
export function getExamDetail(id) {
  return request({
    url: `/v1/exams/${id}`,
    method: 'get'
  })
}

// 获取我的考试
export function getMyExams(params) {
  return request({
    url: '/v1/exams/my',
    method: 'get',
    params
  })
}

// 获取热门考试
export function getHotExams(limit = 10) {
  return request({
    url: '/v1/exams/hot',
    method: 'get',
    params: { limit }
  })
}

// 搜索考试
export function searchExams(keyword, params) {
  return request({
    url: '/v1/exams/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// 检查考试权限
export function checkExamPermission(examId) {
  return request({
    url: `/v1/exams/${examId}/permission`,
    method: 'get'
  })
}

// ==================== 考试记录 ====================

// 开始考试
export function startExam(examId) {
  return request({
    url: '/v1/exam-records/start',
    method: 'post',
    data: { examId }
  })
}

// 获取考试记录
export function getExamRecord(recordId) {
  return request({
    url: `/v1/exam-records/${recordId}`,
    method: 'get'
  })
}

// 提交答案
export function submitAnswer(recordId, questionId, answer) {
  return request({
    url: `/v1/exam-records/${recordId}/answer`,
    method: 'post',
    data: { questionId, answer }
  })
}

// 提交考试
export function submitExam(recordId) {
  return request({
    url: `/v1/exam-records/${recordId}/submit`,
    method: 'post'
  })
}

// 获取我的考试记录
export function getMyExamRecords(params) {
  return request({
    url: '/v1/exam-records/my',
    method: 'get',
    params
  })
}

// 获取考试结果
export function getExamResult(recordId) {
  return request({
    url: `/v1/exam-records/${recordId}/result`,
    method: 'get'
  })
}

// 获取考试统计
export function getExamStatistics() {
  return request({
    url: '/v1/exam-records/statistics',
    method: 'get'
  })
}

// ==================== 题目管理 ====================

// 获取考试题目
export function getExamQuestions(examId) {
  return request({
    url: `/v1/questions/exam/${examId}`,
    method: 'get'
  })
}

// 获取题目详情
export function getQuestionDetail(id) {
  return request({
    url: `/v1/questions/${id}`,
    method: 'get'
  })
}

// 获取练习题目
export function getPracticeQuestions(params) {
  return request({
    url: '/v1/questions/practice',
    method: 'get',
    params
  })
}

// 提交练习答案
export function submitPracticeAnswer(questionId, answer) {
  return request({
    url: `/v1/questions/${questionId}/practice`,
    method: 'post',
    data: { answer }
  })
}

// ==================== 错题本 ====================

// 获取错题列表
export function getWrongQuestions(params) {
  return request({
    url: '/v1/wrong-questions',
    method: 'get',
    params
  })
}

// 添加到错题本
export function addToWrongQuestions(questionId, examRecordId) {
  return request({
    url: '/v1/wrong-questions',
    method: 'post',
    data: { questionId, examRecordId }
  })
}

// 从错题本移除
export function removeFromWrongQuestions(id) {
  return request({
    url: `/v1/wrong-questions/${id}`,
    method: 'delete'
  })
}

// 标记已掌握
export function markAsMastered(id) {
  return request({
    url: `/v1/wrong-questions/${id}/master`,
    method: 'post'
  })
}

// ==================== 考试分析 ====================

// 获取考试分析报告
export function getExamAnalysis(recordId) {
  return request({
    url: `/v1/exam-analysis/${recordId}`,
    method: 'get'
  })
}

// 获取知识点掌握情况
export function getKnowledgePointMastery() {
  return request({
    url: '/v1/exam-analysis/knowledge-points',
    method: 'get'
  })
}

// 获取能力雷达图
export function getAbilityRadar() {
  return request({
    url: '/v1/exam-analysis/ability-radar',
    method: 'get'
  })
}

// ==================== 考试排行榜 ====================

// 获取考试排行榜
export function getExamRanking(examId, params) {
  return request({
    url: `/v1/exam-ranking/${examId}`,
    method: 'get',
    params
  })
}

// 获取总排行榜
export function getTotalRanking(params) {
  return request({
    url: '/v1/exam-ranking/total',
    method: 'get',
    params
  })
}

// 获取我的排名
export function getMyRanking() {
  return request({
    url: '/v1/exam-ranking/my',
    method: 'get'
  })
}
